package com.xhj.springbootproject.listener;

import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.xhj.springbootproject.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ：Xionghaijun
 * @date ：Created in 2019/7/23 9:47 AM
 * @description： 定义任务监听器，统计每次任务执行的时间
 */
public class MyElasticJobListener implements ElasticJobListener {

    private static final Logger logger = LoggerFactory.getLogger(MyElasticJobListener.class);

    private long beginTime = 0;
    @Override
    public void beforeJobExecuted(ShardingContexts shardingContexts) {
        beginTime = System.currentTimeMillis();

        logger.info("===>{} 开始执行定时任务，开始时间: {} <===",shardingContexts.getJobName(), TimeUtil.date(beginTime));
    }

    @Override
    public void afterJobExecuted(ShardingContexts shardingContexts) {
        long endTime = System.currentTimeMillis();

        logger.info("===>{} 定时任务正常结束，结束时间: {},总耗时: {} <===",shardingContexts.getJobName(), TimeUtil.date(endTime), endTime - beginTime);
    }
}
